
package com.be.beadmin.quality.impl;

import com.be.beadmin.product.entity.ErrorReportFileInfo_;
import com.be.beadmin.project.Util.MinioUtil;
import com.be.beadmin.quality.dto.input.ComplaintFileInfoPage;
import com.be.beadmin.quality.entity.ComplaintFileInfo;
import com.be.beadmin.quality.entity.ComplaintFileInfo_;
import cn.hutool.core.util.ObjectUtil;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaSortBuilder;
import com.be.beadmin.quality.entity.ComplaintFileInfo;
import com.be.beadmin.quality.entity.CustomComplaint;
import com.be.beadmin.quality.repository.CustomComplaintRepository;
import com.be.beadmin.quality.service.CustomComplaintService;
import com.be.beadmin.service.service.SerialNumberService;
import lombok.RequiredArgsConstructor;
import com.be.beadmin.quality.mapper.ComplaintFileInfoMapperEx;
import com.be.beadmin.quality.repository.ComplaintFileInfoRepository;
import com.be.beadmin.quality.service.ComplaintFileInfoService;
import com.be.beadmin.quality.dto.input.ComplaintFileInfoDto;
import com.be.beadmin.quality.dto.output.ComplaintFileInfoEx;
import com.be.beadmin.quality.dto.input.ComplaintFileInfoQueryCriteria;
import com.be.beadmin.quality.mapper.ComplaintFileInfoMapper;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Order;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
* @description 服务实现
* @author twl
* @date 2023-09-21
**/
@Service
@RequiredArgsConstructor
public class ComplaintFileInfoServiceImpl implements ComplaintFileInfoService {

    @Autowired
    private  ComplaintFileInfoRepository complaintFileInfoRepository;
    @Autowired
    private CustomComplaintRepository customComplaintRepository;
    @Autowired
    private  ComplaintFileInfoMapper complaintFileInfoMapper;
    @Autowired
    private  ComplaintFileInfoMapperEx complaintFileInfoExMapper;


    @Autowired
    private SerialNumberService serialNumberService;

    MinioUtil minioUtil = new MinioUtil();

    @Override
    public Page<ComplaintFileInfoEx> getComplaintFileInfoByPage(PageEntity pageEntity) {
            Pageable pageable = PageRequest.of(
            pageEntity.getCurrent() -1,
            pageEntity.getPageSize());
            Page<ComplaintFileInfo> page = complaintFileInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilder.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(ComplaintFileInfo_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(ComplaintFileInfo_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
            Page<ComplaintFileInfoEx> page1 = page.map(complaintFileInfoExMapper::toEntity);
            return page1;

    }

    @Override
    public Page<ComplaintFileInfoEx> getComplaintFileInfoByPageAndCP(ComplaintFileInfoPage complaintFileInfoPage) {
        PageEntity pageEntity=complaintFileInfoPage.getPageEntity();
        String complaintId=complaintFileInfoPage.getComplaintId();
        Pageable pageable = PageRequest.of(
                pageEntity.getCurrent() -1,
                pageEntity.getPageSize());
        Page<ComplaintFileInfo> page = complaintFileInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilder.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(ComplaintFileInfo_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate pre3 = criteriaBuilder.equal(root.get(ComplaintFileInfo_.complaintId), complaintId); //过滤为0
                    list.add(pre3);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(ComplaintFileInfo_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                }
                , pageable);
        Page<ComplaintFileInfoEx> page1 = page.map(complaintFileInfoExMapper::toEntity);
        return page1;
    }

    @SneakyThrows
    @Override
    public void addComplaintFileInfo(String complaintId, MultipartFile[] multipartFiles,Integer state,String bucketName) {
        CustomComplaint complaint=customComplaintRepository.findCustomComplaintById(complaintId);
        if(ObjectUtil.isEmpty(complaint)) {
            throw new BeadminException("Complaint不存在,请重新查询");
        }
        String infoId=complaint.getInfoId();
        for(MultipartFile file:multipartFiles) {
            ComplaintFileInfo result = new ComplaintFileInfo();

            result.setComplaintId(complaintId);
            result.setInfoId(infoId);
            result.setState(state);
            result.setFileCode(serialNumberService.getNextSnByCode("FileCode"));
            result.setFileName(file.getOriginalFilename());
            result.setBucketName(bucketName);
            String uploadFileName=result.getFileCode()+"+"+result.getFileName();
            minioUtil.uploadFile(file,uploadFileName,bucketName);
            String fileUrl = minioUtil.getReadFile(bucketName,uploadFileName);
            result.setFileUrl(fileUrl);
            complaintFileInfoRepository.save(result);
        }
    }

    @Override
    public void updateComplaintFileInfo(ComplaintFileInfoDto complaintFileInfoDto) {
            ComplaintFileInfo complaintFileInfo = complaintFileInfoRepository.findComplaintFileInfoById(complaintFileInfoDto.getId());
            complaintFileInfo = complaintFileInfoMapper.toEntity(complaintFileInfoDto);
            complaintFileInfoRepository.save(complaintFileInfo);
    }


    @Override
    public void delComplaintFileInfo(String id) {
        ComplaintFileInfo complaintFileInfo = complaintFileInfoRepository.findComplaintFileInfoById(id);
        complaintFileInfo.setDelete(1);
        complaintFileInfoRepository.save(complaintFileInfo);
    }

    @Override
    public void delComplaintFileInfos (List<String> ids) { }

    @Override
    public void getComplaintFileInfosByComplaintId(String complaintId){

    }
}